package com.bawei.module_publish.openlive.ui

import android.app.Dialog
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.bawei.lib_common.base.BaseApplication
import com.bawei.lib_common.base.utils.LoggerUtils
import com.bawei.module_publish.R
import com.bawei.module_publish.openlive.intent.LiveIntent
import com.bawei.module_publish.openlive.model.entity.TopCarBody
import com.bawei.module_publish.openlive.state.LiveState
import com.bawei.module_publish.openlive.viewmodel.LiveViewModel
import kotlinx.coroutines.launch
import com.bawei.module_publish.openlive.model.entity.GoodRecord
import com.blankj.utilcode.util.ToastUtils

private const val TAG = "ExplainDialog"

class ExplainDialog constructor(
    private val roomId: String,
    private val goodList: MutableList<GoodRecord>,
    private val onExplainSuccess: ((String) -> Unit)? = null
) : DialogFragment() {
    private lateinit var liveViewModel: LiveViewModel

    private var index: Int = 0
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = Dialog(requireContext(), R.style.Base_Theme_IVideo)
        val view = LayoutInflater.from(context).inflate(R.layout.dialog_explain, null)
        liveViewModel = ViewModelProvider(this)[LiveViewModel::class.java]
        dialog.setContentView(view)
        dialog.setCancelable(true)
        dialog.setCanceledOnTouchOutside(true)
        view.findViewById<TextView>(R.id.btnIKnow).setOnClickListener {
            val topCarBody = TopCarBody(roomId, "285417033236483")
            lifecycleScope.launch {
                liveViewModel.intents.send(LiveIntent.ExplainGoods(topCarBody))
            }
            ToastUtils.showShort("你已知晓讲解回放功能")
            // 不立即dismiss，等成功回调
        }
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                liveViewModel._explainGoodsFlow.collect {
                    when (it) {
                        is LiveState.Init -> {
                            LoggerUtils.d(TAG, "初始化成功")
                        }

                        is LiveState.Empty -> {
                            LoggerUtils.d(TAG, "数据为空")
                        }

                        is LiveState.Failed -> {
                            LoggerUtils.d(TAG, "请求失败${it.msg}")
                        }

                        is LiveState.ExplainGoodsSuccess -> {
                            Log.d(TAG, "handleState: ${it.msg}")
//                            EventBus.getDefault().post(ExplainSuccessEvent(goodList,index))
                            dismiss()
                        }

                        else -> {
                            LoggerUtils.d(TAG, "其他状态")
                        }
                    }
                }
            }
        }
        return dialog
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.setLayout(
            (resources.displayMetrics.widthPixels * 0.8).toInt(),
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
    }
} 